using System;
using System.Collections.Generic;
using System.Text;
using CreamX.Framework.Attributes;
using Microsoft.Xna.Framework;

namespace CreamX.Framework.Game.Components
{
    [CreamXComponentAttribute("CreamXBoundsComponent")]
    public class CreamXBoundsComponent:CreamXComponent
    {
        BoundingBox boundingBox;
        public CreamXBoundsComponent() { }

        public override void OnRegister()
        {
            boundingBox = new BoundingBox(new Vector3(TopLeft, 0), new Vector3(BottomRight, 0));
            Enabled = true;
            base.OnRegister();
        }
        public override void Update(float elapsed)
        {
            Vector2 pos = SceneObject.Position - SceneObject.Origin;
            BoundsSide flags=BoundsSide.None;
            if (pos.X < TopLeft.X)
                flags = BoundsSide.Left | flags;
            if (pos.X +SceneObject.DisplayWidth > BottomRight.X)
                flags = BoundsSide.Right| flags;
            if (pos.Y < TopLeft.Y)
                flags = BoundsSide.Top| flags;
            if (pos.Y + SceneObject.DisplayHeight> BottomRight.Y)
                flags = BoundsSide.Bottom| flags;

            if (flags != 0)
            {
                BoundsHit(SceneObject, flags);
            }

            base.Update(elapsed);
        }
        private Vector2 _topLeft;
        [CreamXComponentProperty("TopLeft")]
        public Vector2 TopLeft
        {
            get { return _topLeft; }
            set { _topLeft = value; }
        }

        private Vector2 _bottomRight;
        [CreamXComponentProperty("BottomRight")]
        public Vector2 BottomRight
        {
            get { return _bottomRight; }
            set { _bottomRight = value; }
        }
        public BoundsHitHandler BoundsHit;
    }
    [Flags()]
    public enum BoundsSide
    {
        None=0,
        Top=1,
        Left=2,
        Right=4,
        Bottom=8
    }
    public delegate void BoundsHitHandler(CreamXObject owner,BoundsSide hit);
}
